/*
 * Created on 2004. 11. 22.
 */
package net.okjsp;

import java.io.UnsupportedEncodingException;

import junit.framework.TestCase;

/**
 * @author kenu_at_okjsp.pe.kr
 * 
 */
public class StringHelperTest extends TestCase {
	public void testReadUnicode() {
		String string = "&#44608;&#44049;&#46028;";
		String c2 = StringHelper.readUnicode(string);
		assertEquals("김갑돌", c2);
	}

	public void testReadUnicode2() {
		String string = "&#44608;&#44049; &#46028;";
		String c2 = StringHelper.readUnicode(string);
		assertEquals("김갑 돌", c2);
	}

	public void testReadUnicode3() {
		String string = "abc";
		String c2 = StringHelper.readUnicode(string);
		assertEquals("abc", c2);
	}

	public void testReadNull() {
		String string = null;
		String c2 = StringHelper.readUnicode(string);
		assertEquals(null, c2);
	}

	public void testSplit() {
		String[] strings = "&#44608;&#44049;&#46028;".split("&#");
		assertEquals(4, strings.length);
		assertEquals("", strings[0]);
		assertEquals("44608;", strings[1]);
	}

	public void testCode() {
		String unicode = "&#xAC00;&#xB098;&#xB2E4;&#xB77C;&#xB9C8;";
		assertEquals("가나다라마", StringHelper.readUnicode(unicode));
	}

	public void testCodeMixed() {
		String unicode = "&#xAC00;&#xB098;가나&#xB2E4;&#xB77C;&#xB9C8;";
		assertEquals("가나가나다라마", StringHelper.readUnicode(unicode));
	}

	String string = "자바와 한글";

	String[] charsets = { "ms949", "8859_1", "utf-8" };

	public void testChangeCharSet() throws UnsupportedEncodingException {
		for (int i = 0; i < charsets.length; i++) {
			for (int j = 0; j < charsets.length; j++) {
				if (i == j)
					continue;
				System.out.print(i + ":" + j + " ");
				// String test_str = new String((string).getBytes(charsets[i]), charsets[j]);
				// String test_result = new String((test_str).getBytes(charsets[j]), charsets[i]);
				// printByteList(test_result.getBytes());
			}
		}
	}

	public void testChangeCharSet2() throws UnsupportedEncodingException {
		String test_str = new String((string).getBytes("utf-8"), "8859_1");
		String test_result = new String(test_str.getBytes("8859_1"), "utf-8");
		printByteList(test_result.getBytes());
		assertEquals(string, test_result);
	}
	
	/**
	 * @param test_str
	 */
	private void printByteList(byte[] test_str) {
		for (int i = 0; i < test_str.length; i++) {
			System.out.print((int) test_str[i] + ",");
		}
		System.out.println(new String(test_str));

	}

}
